//
//  SPCollectViewController.m
//  Sport
//
//  Created by Cass on 2017/12/2.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPCollectViewController.h"
#import "SPVenueInfoCell.h"
#import "SPStoreViewController.h"
#import "SPInfoModel.h"
@interface SPCollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *collectArray;
@property (strong, nonatomic) NSUserDefaults *userDefaults;

@property (strong, nonatomic) NSMutableArray<SPInfoModel *> *states;
@end

@implementation SPCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self loadNewData];
    _userDefaults = [NSUserDefaults standardUserDefaults];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView.mj_header beginRefreshing];
    self.navigationItem.title = @"收藏";
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refreshData {
    [_collectArray removeAllObjects];
    if (![_userDefaults objectForKey:@"collect"]) {
        [_userDefaults setObject:self.collectArray forKey:@"collect"];
    }
    for (NSNumber *t in [_userDefaults objectForKey:@"collect"]) {
        [self.collectArray addObject:t];//从_userDefaults中依次取出元素放入collectArray
    }
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

- (NSMutableArray<SPInfoModel *> *)states{
    if (!_states) {
        _states = [[NSMutableArray alloc]init];
    }
    return _states;
}

- (NSMutableArray *)collectArray{
    if (_collectArray == nil) {
        _collectArray = [NSMutableArray array];
    }
    return _collectArray;
}

- (void)loadNewData{
    NSString *url = [NSString stringWithFormat:@"http://39.108.132.194/Sport/venuesinfo.php"];
    [SPHttpClient POST:url parameters:nil success:^(id responseObject) {
        NSArray *info = responseObject;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dict in info) {
            SPInfoModel *state = [SPInfoModel mj_objectWithKeyValues:dict];
            [arr addObject:state];
        }
        self.states  = arr;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.states.count?self.collectArray.count:0;
//    return self.states.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    SPVenueInfoCell *cell = [SPVenueInfoCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    NSNumber *t;
    t = _collectArray[indexPath.row];
    cell.infoModel = self.states[t.integerValue - 1];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SPStoreViewController *storeVC = [[UIStoryboard storyboardWithName:@"Venue" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPStoreViewController class])];
    NSNumber *t;
    t = _collectArray[indexPath.row];
    storeVC.infoModel = self.states[t.integerValue-1];
    [self.navigationController pushViewController:storeVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
